home *** CD-ROM | disk | FTP | other *** search
/ Mac Mania 2 / MacMania 2.toast / Demo's / Tools&Utilities / Programming / FBSpriteWorld 1.05b / Project 1.01b / GraphicUtils.incl < prev    next >
Encoding:
Text File  |  1994-04-26  |  5.6 KB  |  151 lines  |  [TEXT/ZBAS]

  1. 'GraphicUtils.incl by Robert Hommel
  2. '© Copyright 1994
  3. 'All rights granted for any use whatsoever
  4.  
  5. 'Disclaimer:  I've tested these routines quite thoroughly on my Mac
  6. 'LC running System 7.01 and FB 1.02c.  I make no promises or warranties 
  7. 'of any kind.
  8. '*********************************************************************
  9.  
  10. COMPILE 0, _MacsBugLabels _caseInsensitive _STRResource
  11. INCLUDE FILE _aplIncl
  12. DEFSTR LONG
  13. DEFINT A-Z
  14.  
  15. '--------------------------- GLOBALS --------------------------------
  16.  
  17. GLOBALS "GraphicUtils.glbl"
  18.  
  19. END GLOBALS
  20.  
  21. '------------------ OFF SCREEN GRAPHICS UTILS -----------------------
  22.  
  23. CLEAR LOCAL MODE                                  'returns an offscreen GWorld
  24. DIM rect;8                                        'warning: use only with 32-bit color QuickDraw
  25. LOCAL FN GetOffScrnGWorld&(rect;8)
  26.    CALL LOCALTOGLOBAL(#rect.top%)
  27.    CALL LOCALTOGLOBAL(#rect.bottom%)
  28.    QDErr = FN NEWGWORLD(offPort&,0,rect,0,0,0)    'offPort contains offScreen port
  29.    LONG IF QDErr                                  'check for error
  30.       LONG IF offPort&
  31.          CALL DISPOSEGWORLD(offPort&)             'dump GWORLD block if necessary
  32.       END IF
  33.       offPort&=0
  34.    END IF
  35.    CALL GLOBALTOLOCAL(#rect.top%)
  36.    CALL GLOBALTOLOCAL(#rect.bottom%)
  37. END FN = offPort&
  38.  
  39.  
  40.  
  41.  
  42.  
  43. LOCAL MODE                                        'this routine sets up a B&W offscreen bitmap
  44. DIM rect;0,t,l,b,r                                '(works on any Macintosh)
  45. LOCAL FN GetOffScrnPort& (rect;8)          
  46.    CALL GETPORT(currPort&)
  47.    offPort& = FN NEWPTR(_portRec)                 'allocate ptr block (_portRec = GrafPortSize)
  48.    LONG IF offPort& <> 0
  49.       CALL OPENPORT(offPort&)               
  50.       RowBytes = (((r-l)+15)/8) AND &7FFE         'get rowbytes (one pixel deep)
  51.       MapSz& = RowBytes * ((b-t)+1)               'calc memory needed
  52.       LONG IF MapSz& < FN FREEMEM + 32000         '32K extra for good measure
  53.          BLOCKMOVE @rect, offPort&+_portRect, 8   'make port rect the correct size
  54.          VisRgn& = [offPort&+_visRgn]             'get visible region
  55.          CALL RECTRGN(VisRgn&,rect)               'make it our rect
  56.          ClipRgn& = [offPort&+_clipRgn]           'do same for clip region
  57.          CALL RECTRGN(ClipRgn&,rect)
  58.          Map& = FN NEWPTR(MapSz&)                 'pointer for bitmap and screenbits
  59.          LONG IF Map& <> 0
  60.             offPort&.portBits& = Map&             'set map as portbits (B&W bitmap)
  61.             offPort&.portBits.rowBytes% = RowBytes'needs to know width in bytes
  62.             BLOCKMOVE @rect, offPort&+_portBits+_bounds, 8'needs rect too
  63.          END IF
  64.       XELSE                                       'Not enough memory here...
  65.          CALL CLOSEPORT(offPort&)                 'not enought memory some dump port
  66.          OSErr = FN DISPOSPTR(offPort&)           'and its pointer
  67.       END IF
  68.    END IF
  69.    CALL SETPORT(currPort&)                        'point us back to where we were
  70. END FN = offPort&
  71.  
  72.  
  73.  
  74.  
  75. LOCAL MODE                                        'this routine gets a pointer to a port/gWorld
  76. DIM wndPort&, Gdevice&                            'for GETGWORLD
  77. LOCAL FN GetCurrPort                    
  78.    LONG IF gColorDepth = 32                       'do we have 32-bit QuickDraw?
  79.       CALL GETGWORLD(wndPort&,Gdevice&)           'set port to our new gWorld
  80.    XELSE
  81.       CALL GETPORT(wndPort&)                      'get pointer to window’s GrafPort
  82.    END IF
  83. END FN=wndPort&
  84.  
  85.  
  86.  
  87.  
  88. LOCAL MODE                                        'routine sets output to a specific port/gWorld
  89. LOCAL FN SetThePort (thePort&)                    '(depends on color or black and white)
  90.    LONG IF gColorDepth = 32                       'do we have 32-bit QuickDraw?
  91.       gDev& = FN GETGWORLDDEVICE(thePort&)
  92.       CALL SETGWORLD(thePort&,0)                  'set port to offScreen GWorld
  93.    XELSE 
  94.       CALL SETPORT(thePort&)                      'else do it the old way
  95.    END IF
  96. END FN
  97.  
  98.  
  99.  
  100.  
  101. LOCAL MODE                                        'copy bitmap from offscreen to window
  102. DIM sRect.8,dRect.8
  103. LOCAL FN CopyOffScreenBits (offPort&,wndPort&,sRect;8,dRect;8,copyMode,maskRgn&)
  104.    LONG IF gColorDepth = 32                       'if 32-bit QuickDraw...
  105.       locked = FN LOCKPIXELS(FN GETGWORLDPIXMAP(offPort&))
  106.       LONG IF locked
  107.          CALL COPYBITS(#offPort&+2,#wndPort&+2,sRect,dRect,copyMode,maskRgn&)
  108.          CALL UNLOCKPIXELS(FN GETGWORLDPIXMAP(offPort&))
  109.       END IF
  110.    XELSE                                          'do this if not 32-bit QD
  111.       CALL COPYBITS(#offPort&+2,#wndPort&+2,sRect,dRect,copyMode,maskRgn&)
  112.    END IF
  113. END FN
  114.  
  115.  
  116.  
  117.  
  118. CLEAR LOCAL 
  119. LOCAL FN DisposeGrafPort(grafPtr&)
  120.    osErr=0
  121.    LONG IF gColorDepth = 32
  122.       CALL DISPOSEGWORLD(grafPtr&)                'dump offscreen GWORLD block
  123.    XELSE
  124.       CALL CLOSEPORT(grafPtr&)                    'dump offscreen B&W port
  125.       osErr = FN DISPOSPTR(grafPtr&)              'and its other stuff
  126.    END IF
  127. END FN=osErr
  128.  
  129.  
  130.  
  131. '---------------------- GENERAL GRAPHIC UTILS -----------------------
  132.  
  133.  
  134. CLEAR LOCAL
  135. LOCAL FN CenterRect(@smallRectPtr&,@bigRectPtr&)
  136.    'sets smallRect to a rectangle centered inside of bigRect. Does nothing
  137.    'if smallRect>=bigRect.
  138.    
  139.    smallRectV=smallRectPtr&.bottom%-smallRectPtr&.top%
  140.    smallRectH=smallRectPtr&.right%-smallRectPtr&.left%
  141.    bigRectV=bigRectPtr&.bottom%-bigRectPtr&.top%
  142.    bigRectH=bigRectPtr&.right%-bigRectPtr&.left%
  143.    LONG IF smallRectV<bigRectV AND smallRectH<bigRectH
  144.       %smallRectPtr&+_top,bigRectPtr&.top%+INT(.5*(bigRectV-smallRectV))
  145.       %smallRectPtr&+_left,bigRectPtr&.left%+INT(.5*(bigRectH-smallRectH))
  146.       %smallRectPtr&+_bottom,smallRectPtr&.top%+smallRectV
  147.       %smallRectPtr&+_right,smallRectPtr&.left%+smallRectH
  148.    END IF
  149. END FN
  150.  
  151.